package com.sfzd5.exam.helplibrary.exam;

import com.google.zxing.WriterException;
import com.sfzd5.exam.helplibrary.DrawMatHelper;
import com.sfzd5.exam.helplibrary.QrcodeHelper;
import org.bytedeco.javacpp.opencv_core.*;

/**
 * 字号处理办法
 * 高度即字体大小
 * 坐标计算 实际Y度假村，y = y + h - fm.getDescent();
 * Word字号高度计算：字号*25.4/72 ，单位 毫米，Word字号即点数
 * 毫米到点的计算：300*mm/25.4
 *
 * 目标，设计一个以毫米为单位的输出平台
 * 构建函数：
 * 1.输出方块，x,y,w,h
 * 2.输出文字，x,y,h
 * 3.输出图片，x,y,w,h
 * 4.输出方框，x,y,w,h
 * 5.画线, x1,y1,x2,y2
 */
public class Graphics2DMM {

    private DrawMatHelper g;

    public DrawMatHelper getDrawMatHelper(){
        return g;
    }

    public Graphics2DMM(int width, int height)
    {
        this.g = new DrawMatHelper(width, height);
    }

    public void drawLine(float mx1, float my1, float mx2, float my2, float lineWidthm){


        float lineWidth = PageA4.mm2point(lineWidthm);
        //if(lineWidth==0) lineWidth=1;

        int x1 = (int)mm2point(mx1);
        int y1 = (int)mm2point(my1);
        int x2 = (int)mm2point(mx2);
        int y2 = (int)mm2point(my2);
        g.setStroke(lineWidth);
        g.drawLine(x1, y1, x2, y2);
    }

    public void drawRect(float mx, float my, float mw, float mh, float rectBoardWidthm){
        float rectBoardWidth = PageA4.mm2point(rectBoardWidthm);
        //if(rectBoardWidth==0) rectBoardWidth=1;
        float x = PageA4.mm2point(mx);
        float y = PageA4.mm2point(my);
        float w = PageA4.mm2point(mw);
        float h = PageA4.mm2point(mh);//即fontsize

        g.setStroke(rectBoardWidth);
        g.drawRect(x, y, w, h);
    }

    public void drawImage(Mat image, float mx, float my, float mw, float mh){
        float x = PageA4.mm2point(mx);
        float y = PageA4.mm2point(my);
        float w = PageA4.mm2point(mw);
        float h = PageA4.mm2point(mh);//即fontsize
        g.drawImage(image, (int)x, (int)y, (int)w, (int)h);
    }

    public void drawCodeEncode(String encodeddata, float mx, float my, float sizem, char errorCorrect, char mode, int version) {

        int x = (int)mm2point(mx);
        int y = (int)mm2point(my);
        float size = mm2point(sizem);

        try {
            Mat qrMat = QrcodeHelper.qrCodeEncode(encodeddata, (int)size);
            // 定义感兴趣区域(位置，logo图像大小)
            Mat ROI = g.getMat().apply(new Rect((int)x, (int)y, (int)size, (int)size));

            qrMat.copyTo(ROI);
        } catch (WriterException e) {
            e.printStackTrace();
        }
    }

    public void drawCodeEncode(String encodeddata, float mx, float my, float sizem) {
        drawCodeEncode(encodeddata, mx, my, sizem, 'L', 'B', 0);
    }


    /**
     * 水平和垂直居中划线
     * @param str
     * @param mx
     * @param my
     * @param mh
     */
    public void drawString(String str, float mx, float my, float mh){
        float x = PageA4.mm2point(mx);
        float y = PageA4.mm2point(my);
        float h = PageA4.mm2point(mh);//即fontsize

        g.drawString(str, x, y, -1, h);
    }

    /**
     * 水平和垂直居中划线
     * @param str
     * @param mx
     * @param my
     * @param mh
     * @param mw
     */
    public void drawTtitleString(String str, float mx, float my, float mw, float mh){
        float x = PageA4.mm2point(mx);
        float y = PageA4.mm2point(my);
        float w = PageA4.mm2point(mw);
        float h = PageA4.mm2point(mh);//即fontsize

        g.drawString(str, x, y, w, h);
    }

    public void fillRect(float mx, float my, float mw, float mh){
        g.fillRect((int)PageA4.mm2point(mx), (int)PageA4.mm2point(my), (int)PageA4.mm2point(mw), (int)PageA4.mm2point(mh));
    }

    private float mm2point(float mm){
        return PageA4.mm2point(mm);
    }

}
